package com.smartum.mpr.services;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.smartum.mpr.business.StaticDataBusiness;
import com.smartum.mpr.model.Category;
import com.smartum.mpr.model.Country;
import com.smartum.mpr.model.ItemCategory;
import com.smartum.mpr.model.ItemSubCategory;
import com.smartum.mpr.model.JobCategory;
import com.smartum.mpr.model.JobSubCategory;
import com.smartum.mpr.model.State;
import com.smartum.mpr.model.County;

@Controller
@RequestMapping("/static")
public class StaticDataService extends BaseService<ItemCategory> {

	@Autowired
	private StaticDataBusiness dataBusiness;

	@RequestMapping(value = "/item/categories", method = RequestMethod.POST)
	public ResponseEntity<String> getItemCategoriesList(@RequestBody String json) {
		LOGGER_API.info("request: get item categories list - " + json);
		try {
			checkRequestPermissions(json);

			ResponseEntity<String> response = getResponseInJsonType(
			        generateSuccessJson(getParserByEntityClass(ItemCategory.class).writeEntityList(
			                dataBusiness.getItemCategories())), HttpStatus.OK);
			LOGGER_API.info("response: get item categories list OK");
			logResponse(response);
			return response;
		} catch (Exception e) {
			LOGGER_API.error("response: get item categories list ", e);
			return handleException(e);
		}

	}

	@RequestMapping(value = "/item/subcategories", method = RequestMethod.POST)
	public ResponseEntity<String> getItemSubCategoriesList(@RequestBody String json) {
		LOGGER_API.info("request: get item subcategories list - " + json);
		try {

			checkRequestPermissions(json);
			String request = extractRequestStringData(json);
			ObjectMapper mapper = getTargetParser().getMapper();
			Long categoryID = mapper.readTree(request).get("category_id").asLong();

			ResponseEntity<String> response = getResponseInJsonType(
			        generateSuccessJson(getParserByEntityClass(ItemSubCategory.class).writeEntityList(
			                dataBusiness.getItemSubCategories(categoryID))), HttpStatus.OK);
			LOGGER_API.info("response: get item subcategory list OK");
			logResponse(response);

			return response;
		} catch (Exception e) {
			LOGGER_API.error("response: get item subcategories list ", e);
			return handleException(e);
		}
	}

	@RequestMapping(value = "/job/categories", method = RequestMethod.POST)
	public ResponseEntity<String> getJobCategoriesList(@RequestBody String json) {
		LOGGER_API.info("request: get job categories list - " + json);
		try {
			checkRequestPermissions(json);

			ResponseEntity<String> response = getResponseInJsonType(
			        generateSuccessJson(getParserByEntityClass(JobCategory.class).writeEntityList(
			                dataBusiness.getJobCategories())), HttpStatus.OK);
			LOGGER_API.info("response: get job categories list OK");
			logResponse(response);
			return response;
		} catch (Exception e) {
			LOGGER_API.error("response: get job categories list ", e);
			return handleException(e);
		}

	}

	@RequestMapping(value = "/job/subcategories", method = RequestMethod.POST)
	public ResponseEntity<String> getJobSubCategoriesList(@RequestBody String json) {
		LOGGER_API.info("request: get job subcategories list - " + json);
		try {
			checkRequestPermissions(json);
			String request = extractRequestStringData(json);
			ObjectMapper mapper = getTargetParser().getMapper();
			Long categoryID = mapper.readTree(request).get("category_id").asLong();

			ResponseEntity<String> response = getResponseInJsonType(
			        generateSuccessJson(getParserByEntityClass(JobSubCategory.class).writeEntityList(
			                dataBusiness.getJobSubCategories(categoryID))), HttpStatus.OK);
			LOGGER_API.info("response: get job subcategory list OK");
			logResponse(response);
			return response;
		} catch (Exception e) {
			LOGGER_API.error("response: get job subcategories list ", e);
			return handleException(e);
		}
	}

	@RequestMapping(value = "/item/category/add", method = RequestMethod.POST)
	public ResponseEntity<String> addItemCategory(@RequestBody String json) {
		LOGGER_API.info("request: add item category - " + json);
		try {
			checkRequestPermissions(json);

			String request = extractRequestStringData(json);
			ObjectMapper mapper = getTargetParser().getMapper();
			String name = mapper.readTree(request).get("category_name").asText();
			dataBusiness.addItemCategory(name);

			LOGGER_API.info("response: add item category OK");
			return new ResponseEntity<String>(HttpStatus.OK);
		} catch (Exception e) {
			LOGGER_API.error("response: add item category ", e);
			return handleException(e);
		}

	}

	@RequestMapping(value = "/item/subcategory/add", method = RequestMethod.POST)
	public ResponseEntity<String> addItemSubCategory(@RequestBody String json) {
		LOGGER_API.info("request: add item subcategory - " + json);
		try {
			checkRequestPermissions(json);

			String request = extractRequestStringData(json);
			ObjectMapper mapper = getTargetParser().getMapper();
			Long categoryID = mapper.readTree(request).get("category_id").asLong();
			String subCategoryName = mapper.readTree(request).get("subcategory_name").asText();
			dataBusiness.addItemSubCategory(categoryID, subCategoryName);

			LOGGER_API.info("response: add item subcategory  OK");
			return new ResponseEntity<String>(HttpStatus.OK);
		} catch (Exception e) {
			LOGGER_API.error("response: add item subcategory ", e);
			return handleException(e);
		}
	}

	@RequestMapping(value = "/item/category/update", method = RequestMethod.POST)
	public ResponseEntity<String> updateItemCategory(@RequestBody String json) {
		LOGGER_API.info("request: update item category - " + json);
		try {
			checkRequestPermissions(json);

			String request = extractRequestStringData(json);
			ObjectMapper mapper = getTargetParser().getMapper();
			String name = mapper.readTree(request).get("category_name").asText();
			Long id = mapper.readTree(request).get("category_id").asLong();
			dataBusiness.updateItemCategory(id, name);
			LOGGER_API.info("response: update item category OK");
			return new ResponseEntity<String>(HttpStatus.OK);
		} catch (Exception e) {
			LOGGER_API.error("response: update item category ", e);
			return handleException(e);
		}

	}

	@RequestMapping(value = "/item/subcategory/update", method = RequestMethod.POST)
	public ResponseEntity<String> updateItemSubCategory(@RequestBody String json) {
		LOGGER_API.info("request: update item subcategory - " + json);
		try {
			checkRequestPermissions(json);

			String request = extractRequestStringData(json);
			ObjectMapper mapper = getTargetParser().getMapper();
			Long subCategoryID = mapper.readTree(request).get("subcategory_id").asLong();
			String subCategoryName = mapper.readTree(request).get("subcategory_name").asText();
			dataBusiness.updateItemSubCategory(subCategoryID, subCategoryName);
			LOGGER_API.info("response: update item subcategory  OK");
			return new ResponseEntity<String>(HttpStatus.OK);
		} catch (Exception e) {
			LOGGER_API.error("response: update item subcategory ", e);
			return handleException(e);
		}
	}

	@RequestMapping(value = "/item/category/delete", method = RequestMethod.POST)
	public ResponseEntity<String> deleteItemCategory(@RequestBody String json) {
		LOGGER_API.info("request: delete item category - " + json);
		try {
			checkRequestPermissions(json);

			String request = extractRequestStringData(json);
			ObjectMapper mapper = getTargetParser().getMapper();
			Long id = mapper.readTree(request).get("category_id").asLong();
			dataBusiness.deleteItemCategory(id);
			LOGGER_API.info("response: delete item category OK");
			return new ResponseEntity<String>(HttpStatus.OK);
		} catch (Exception e) {
			LOGGER_API.error("response: delete item category ", e);
			return handleException(e);
		}

	}

	@RequestMapping(value = "/item/subcategory/delete", method = RequestMethod.POST)
	public ResponseEntity<String> deleteItemSubCategory(@RequestBody String json) {
		LOGGER_API.info("request: delete item subcategory - " + json);
		try {
			checkRequestPermissions(json);
		
			String request = extractRequestStringData(json);
			ObjectMapper mapper = getTargetParser().getMapper();
			Long subcategoryId = mapper.readTree(request).get("subcategory_id").asLong();
			dataBusiness.deleteItemSubCategory(subcategoryId);

			LOGGER_API.info("response: delete item subcategory  OK");
			return new ResponseEntity<String>(HttpStatus.OK);
		} catch (Exception e) {
			LOGGER_API.error("response: delete item subcategory ", e);
			return handleException(e);
		}
	}

	@RequestMapping(value = "/job/category/add", method = RequestMethod.POST)
	public ResponseEntity<String> addJobCategory(@RequestBody String json) {
		LOGGER_API.info("request: add job category - " + json);
		try {
			checkRequestPermissions(json);

			String request = extractRequestStringData(json);
			ObjectMapper mapper = getTargetParser().getMapper();
			String name = mapper.readTree(request).get("category_name").asText();
			dataBusiness.addJobCategory(name);

			LOGGER_API.info("response: add job category  OK");
			return new ResponseEntity<String>(HttpStatus.OK);
		} catch (Exception e) {
			LOGGER_API.error("response: add job category ", e);
			return handleException(e);
		}

	}

	@RequestMapping(value = "/job/subcategory/add", method = RequestMethod.POST)
	public ResponseEntity<String> addJobSubCategory(@RequestBody String json) {
		LOGGER_API.info("request: add job subcategory - " + json);
		try {
			checkRequestPermissions(json);
			String request = extractRequestStringData(json);
			ObjectMapper mapper = getTargetParser().getMapper();
			Long categoryID = mapper.readTree(request).get("category_id").asLong();
			String subCategoryName = mapper.readTree(request).get("subcategory_name").asText();
			dataBusiness.addJobSubCategory(categoryID, subCategoryName);

			LOGGER_API.info("response: add job subcategory OK");
			return new ResponseEntity<String>(HttpStatus.OK);
		} catch (Exception e) {
			LOGGER_API.error("response: add job subcategory ", e);
			return handleException(e);
		}
	}

	@RequestMapping(value = "/job/category/update", method = RequestMethod.POST)
	public ResponseEntity<String> updateJobCategory(@RequestBody String json) {
		LOGGER_API.info("request: update job category - " + json);
		try {
			checkRequestPermissions(json);
			String request = extractRequestStringData(json);
			ObjectMapper mapper = getTargetParser().getMapper();
			String name = mapper.readTree(request).get("category_name").asText();
			Long categoryId = mapper.readTree(request).get("category_id").asLong();
			dataBusiness.updateJobCategory(categoryId, name);
			LOGGER_API.info("response: update job category  OK");
			return new ResponseEntity<String>(HttpStatus.OK);
		} catch (Exception e) {
			LOGGER_API.error("response: update job category ", e);
			return handleException(e);
		}

	}

	@RequestMapping(value = "/job/subcategory/update", method = RequestMethod.POST)
	public ResponseEntity<String> updateJobSubCategory(@RequestBody String json) {
		LOGGER_API.info("request: update job subcategory - " + json);
		try {
			checkRequestPermissions(json);
			String request = extractRequestStringData(json);
			ObjectMapper mapper = getTargetParser().getMapper();
			Long subcategoryID = mapper.readTree(request).get("subcategory_id").asLong();
			String subCategoryName = mapper.readTree(request).get("subcategory_name").asText();
			dataBusiness.updateJobSubCategory(subcategoryID, subCategoryName);
			
			LOGGER_API.info("response: update job subcategory OK");
			return new ResponseEntity<String>(HttpStatus.OK);
		} catch (Exception e) {
			LOGGER_API.error("response: update job subcategory ", e);
			return handleException(e);
		}
	}

	@RequestMapping(value = "/job/category/delete", method = RequestMethod.POST)
	public ResponseEntity<String> deleteJobCategory(@RequestBody String json) {
		LOGGER_API.info("request: update job category - " + json);
		try {
			checkRequestPermissions(json);
			String request = extractRequestStringData(json);
			ObjectMapper mapper = getTargetParser().getMapper();
			Long categoryId = mapper.readTree(request).get("category_id").asLong();
			dataBusiness.deleteJobCategory(categoryId);
			LOGGER_API.info("response: update job category  OK");
			return new ResponseEntity<String>(HttpStatus.OK);
		} catch (Exception e) {
			LOGGER_API.error("response: update job category ", e);
			return handleException(e);
		}

	}

	@RequestMapping(value = "/job/subcategory/delete", method = RequestMethod.POST)
	public ResponseEntity<String> deleteJobSubCategory(@RequestBody String json) {
		LOGGER_API.info("request: update job subcategory - " + json);
		try {
			checkRequestPermissions(json);
			String request = extractRequestStringData(json);
			ObjectMapper mapper = getTargetParser().getMapper();
			Long subCategoryId = mapper.readTree(request).get("subcategory_id").asLong();
			dataBusiness.deleteJobSubCategory(subCategoryId);

			LOGGER_API.info("response: update job subcategory OK");
			return new ResponseEntity<String>(HttpStatus.OK);
		} catch (Exception e) {
			LOGGER_API.error("response: update job subcategory ", e);
			return handleException(e);
		}
	}

	@RequestMapping(value = "/counties", method = RequestMethod.POST)
	public ResponseEntity<String> getCountiesList(@RequestBody String json) {
		LOGGER_API.info("request: get counties list - " + json);
		try {
			checkRequestPermissions(json);
			String request = extractRequestStringData(json);
			ObjectMapper mapper = getTargetParser().getMapper();
			Long stateID = mapper.readTree(request).get("state_id").asLong();

			ResponseEntity<String> response = getResponseInJsonType(
			        generateSuccessJson(getParserByEntityClass(County.class).writeEntityList(
			                dataBusiness.getCounties(stateID))), HttpStatus.OK);
			LOGGER_API.info("response: get counties list OK");
			logResponse(response);
			return response;
		} catch (Exception e) {
			LOGGER_API.error("response: get counties list ", e);
			return handleException(e);
		}
	}

	@RequestMapping(value = "/states", method = RequestMethod.POST)
	public ResponseEntity<String> getStateList(@RequestBody String json) {
		LOGGER_API.info("request: get state list - " + json);
		try {
			checkRequestPermissions(json);

			ResponseEntity<String> response = getResponseInJsonType(
			        generateSuccessJson(getParserByEntityClass(State.class).writeEntityList(dataBusiness.getStates())),
			        HttpStatus.OK);
			LOGGER_API.info("response: get state list OK");
			logResponse(response);
			return response;
		} catch (Exception e) {
			LOGGER_API.error("response: get state list ", e);
			return handleException(e);
		}
	}

	@RequestMapping(value = "/countries", method = RequestMethod.POST)
	public ResponseEntity<String> getCountriesList(@RequestBody String json) {
		LOGGER_API.info("request: get country list - " + json);
		try {
			checkRequestPermissions(json);
			List<Country> countries = dataBusiness.getCountries();
			ResponseEntity<String> response = getResponseInJsonType(
			        generateSuccessJson(getParserByEntityClass(Country.class).writeEntityList(countries)),
			        HttpStatus.OK);
			LOGGER_API.info("response: get country list OK");
			logResponse(response);
			return response;
		} catch (Exception e) {
			LOGGER_API.error("response: get country list ", e);
			return handleException(e);
		}
	}

	@Override
	protected Class<ItemCategory> getEntityClass() {
		return ItemCategory.class;
	}

}
